package com.drama.api.task;

import cn.hutool.core.date.DateUtil;
import com.drama.api.company.dramas.dao.CompanyDramasDao;
import com.drama.api.company.dramas.domain.po.CompanyDramasPO;
import com.drama.api.company.message.dao.CompanyMessageDao;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Log4j2
public class CompanyDramaThread extends Thread {

    private CompanyMessageDao companyMessageDao;

    @Autowired
    private CompanyDramasDao companyDramasDao;

    public CompanyDramaThread(CompanyMessageDao companyMessageDao, CompanyDramasDao companyDramasDao){
        this.companyMessageDao=companyMessageDao;
        this.companyDramasDao=companyDramasDao;

    }

    @Override
    public void run() {
        Example example=new Example(CompanyDramasPO.class);
        String today=DateUtil.today();
        log.info("today:::::::::::::::::::::::::::"+today);
        example.and().andEqualTo("dramaExpireState",2).andBetween("dramaDateExpire","1999-01-01 00:00:00",today+" 23:59:59");
        List<CompanyDramasPO> companyDramasPOList= companyDramasDao.selectByExample(example);
        if(companyDramasPOList!=null&&companyDramasPOList.size()>0){
            CompanyDramasPO update=new CompanyDramasPO();
            update.setDramaExpireState(1);
            companyDramasDao.updateByExampleSelective(update,example);
            for (CompanyDramasPO companyDramasPO:companyDramasPOList){
                new CompanyMessageThread(companyMessageDao, companyDramasPO.getCompanyId(),"版权到期通知",
                        "剧目："+companyDramasPO.getDramaName()+"的版权已到期" ,"/drama",1,"").start();
            }
        }

        String newDate=DateUtil.format(DateUtil.offsetDay(DateUtil.parseDate(today),180)," yyyy-MM-dd") ;
        log.info("newDate:::::::::::::::::::::::::::"+newDate);
        Example newExample=new Example(CompanyDramasPO.class);
        newExample.and().andEqualTo("dramaExpireState",2).andBetween("dramaDateExpire",newDate+" 00:00:00",newDate+" 23:59:59");
        List<CompanyDramasPO> companyDramasList= companyDramasDao.selectByExample(newExample);
        if(companyDramasList!=null&&companyDramasList.size()>0){
            for (CompanyDramasPO companyDramasPO:companyDramasList){
                new CompanyMessageThread(companyMessageDao, companyDramasPO.getCompanyId(),"版权到期通知",
                        "剧目："+companyDramasPO.getDramaName()+"的版权距离到期日期还剩180天，情注意！" ,"/drama",1,"").start();
            }
        }
    }
}
